package gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.MenuShortcut;

import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.JToolBar;

import org.jhotdraw.contrib.Desktop;
import org.jhotdraw.contrib.SplitPaneDesktop;
import org.jhotdraw.contrib.SplitPaneDrawApplication;
import org.jhotdraw.framework.DrawingView;
import org.jhotdraw.standard.AbstractCommand;
import org.jhotdraw.standard.ChangeAttributeCommand;
import org.jhotdraw.standard.NullTool;
import org.jhotdraw.util.ColorMap;
import org.jhotdraw.util.Command;
import org.jhotdraw.util.CommandMenu;
import org.jhotdraw.util.UndoManager;
import org.jhotdraw.util.UndoableCommand;

public class PhotoViewerApplication extends SplitPaneDrawApplication {

	
	public static void main(String[] args) {
		PhotoViewerApplication app = new PhotoViewerApplication();
		app.open();

	}
	
	
	
	public PhotoViewerApplication() {
		
		
	}
	
	
	//Methods Overriden from DrawApplication
	
	@Override
	protected Desktop createDesktop() {
		return new PhotoViewerDesktop();
	}
	
	@Override
	protected void createMenus(JMenuBar mb) {
		addMenuIfPossible(mb, createFileMenu());
		//addMenuIfPossible(mb, createEditMenu());
		//addMenuIfPossible(mb, createAlignmentMenu());
		//addMenuIfPossible(mb, createAttributesMenu());
		//addMenuIfPossible(mb, createDebugMenu());
	}
	
	@Override
	protected JMenu createFileMenu() {
		CommandMenu menu = new CommandMenu("File");
		Command cmd = new AbstractCommand("New Remote Connection...", this, false) {
			public void execute() {
				promptOpen();																//override this and create a useful enty for connection details
			}
		};
		menu.add(cmd, new MenuShortcut('n'));
		menu.addSeparator();
		cmd = new AbstractCommand("Exit", this, true) {
			public void execute() {
				endApp();
			}
		};
		menu.add(cmd);
		return menu;
	}


	@Override
	protected void open(final DrawingView newDrawingView) {
		getVersionControlStrategy().assertCompatibleVersion();
		setUndoManager(new UndoManager());
		setIconkit(createIconkit());
		getContentPane().setLayout(new BorderLayout());

		// status line must be created before a tool is set
		setStatusLine(createStatusLine());
		getContentPane().add(getStatusLine(), BorderLayout.SOUTH);

		// create dummy tool until the default tool is activated during toolDone()
		setTool(new NullTool(this), "");
		setView(newDrawingView);

		JToolBar tools = createToolPalette();
		createTools(tools);

		JPanel activePanel = new JPanel();
		activePanel.setAlignmentX(LEFT_ALIGNMENT);
		activePanel.setAlignmentY(TOP_ALIGNMENT);
		activePanel.setLayout(new BorderLayout());
		//activePanel.add(tools, BorderLayout.NORTH);
		setDesktopListener(createDesktopListener());
		setDesktop(createDesktop());
		activePanel.add((Component)getDesktop(), BorderLayout.CENTER);
		getContentPane().add(activePanel, BorderLayout.CENTER);

		JMenuBar mb = new JMenuBar();
		createMenus(mb);
		setJMenuBar(mb);

		Dimension d = defaultSize();
		if (d.width > mb.getPreferredSize().width) {
			setSize(d.width, d.height);
		}
		else {
			setSize(mb.getPreferredSize().width, d.height);
		}
		addListeners();
		setStorageFormatManager(createStorageFormatManager());

		//no work allowed to be done on GUI outside of AWT thread once
		//setVislble(true) must be called before drawing added to desktop, else 
		//DND will fail. on drawing added before with a NPE.  note however that
		//a nulldrawingView will not fail because it is never really added to the desltop
		setVisible(true);
		Runnable r = new Runnable() {
			public void run() {
				if (newDrawingView.isInteractive()) {
					getDesktop().addToDesktop(newDrawingView , Desktop.PRIMARY);
				}
				toolDone();
			}
		};

		if (java.awt.EventQueue.isDispatchThread() == false) {
			try {
				java.awt.EventQueue.invokeAndWait(r);
			}
			catch(java.lang.InterruptedException ie) {
				System.err.println(ie.getMessage());
				exit();
			}
			catch(java.lang.reflect.InvocationTargetException ite) {
				System.err.println(ite.getMessage());
				exit();
			}
		}
		else {
			r.run();
		}

		toolDone();
	}
	
	

	
		
	
	
	

	
	
	
	
}
	
	
	
	
	
	
	
	

